package com.shop.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.shop.entity.ShopProduct;
import com.shop.entity.ShopProductSpecification;
import com.shop.entity.ShopProductTemplate;
import com.shop.mapper.ShopProductSpecificationMapper;
import com.shop.service.ShopProductService;
import com.shop.service.ShopProductSpecificationService;
import com.shop.service.ShopProductTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 服务层实现。
 *
 * @author 彭新才
 * @since 2024-10-12
 */
@Service
public class ShopProductSpecificationServiceImpl extends ServiceImpl<ShopProductSpecificationMapper, ShopProductSpecification> implements ShopProductSpecificationService {

    @Autowired
    ShopProductService productService;
    @Autowired
    ShopProductTemplateService productTemplateService;

    @Override
    public boolean add(ShopProductSpecification shopProductSpecification) {
        Integer productId = shopProductSpecification.getProductId();
        ShopProduct product = productService.getById(productId);
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }

        Integer templateId = shopProductSpecification.getTemplateId();
        ShopProductTemplate productTemplate = productTemplateService.getById(templateId);
        if (productTemplate == null) {
            throw new RuntimeException("模板不存在");
        }
        return save(shopProductSpecification);
    }
}
